module rank {

    export class RecentRank extends egret.Sprite {

        public static readonly WIDTH: number = 70;
        public static readonly HEIGHT: number = 70;

        private headImg: egret.Bitmap;
        private info: ScoreItem;
        private nickTxt;


        public constructor(info: ScoreItem, bgColor: number = 0x3D5F1A) {
            super();
            this.info = info;
            this.graphics.drawRect(0, 0, RecentRank.WIDTH, RecentRank.HEIGHT);
            this.graphics.endFill();
            if (info != null) {
                this.init();
            }
        }

        public init(): void {
            let info = this.info
            
            this.headImg = new egret.Bitmap();
            this.headImg.width = 60;
            this.headImg.height = 60;
            this.addChild(this.headImg);

            let imageLoader: egret.ImageLoader = new egret.ImageLoader();
            imageLoader.addEventListener(egret.Event.COMPLETE, this.loadCompleteHandler, this);
            imageLoader.load(this.info.avatarUrl); 

            this.nickTxt = new egret.TextField();
            this.nickTxt.text = info.KVDataList[0].value;
            this.nickTxt.textColor = 0xffffff;
            this.nickTxt.width = this.headImg.width + 20
            this.nickTxt.x = -10;
            this.nickTxt.y = 70;
            this.nickTxt.textAlign = egret.HorizontalAlign.CENTER;
            this.addChild(this.nickTxt);
        }

        private loadCompleteHandler(event: egret.Event): void {
            let imageLoader = <egret.ImageLoader>event.currentTarget;
            imageLoader.removeEventListener(egret.Event.COMPLETE, this.loadCompleteHandler, this);
            let texture = new egret.Texture();
            texture._setBitmapData(imageLoader.data);
            this.headImg.texture = texture;
        }
    }
}